package mat.simul;

public class SimAccesParkingI2c
{
  private SimAccesParking simAccesParking;
  private int numeroAcces;

  public SimAccesParkingI2c()
  {

  }


  public void setNumeroAcces(int num)
  {
      numeroAcces = num;
      if (simAccesParking != null) simAccesParking.setNumeroAcces(num);
  }
  public int getNumeroAcces()
  {
      return numeroAcces;
  }

  public void setBusConnexion(boolean on)
  {
      if(on)
      {
	  if( simAccesParking == null)simAccesParking = new SimAccesParking(numeroAcces);
      }
      else
      {
	  if( simAccesParking != null)
	  {
	      simAccesParking.kill();
	      simAccesParking = null;
	  }
      }
  }

  public boolean getBusConnexion()
  {
      return simAccesParking != null;
  }

  public int doTrame(byte[] trame)
  {
     int firstByte = trame[0] & 0xff;
     boolean bitWrite = (firstByte & 1) == 0;
     int adresse = firstByte >> 1;
     if (bitWrite) return write(adresse, trame, 1, trame.length -1);
     else  return read(adresse, trame, 1, trame.length - 1);
  }



  //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  //       implementation de l'interface BusI2c
  //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


  public int getClockDelay()
  {
    return 0;
  }

  public void setClockDelay(int delai) {}

  public  int write(int adresse, byte[] barr, int off, int len)
  {
     int result = -1;
     if (simAccesParking != null)
     {
	  result = simAccesParking.write(adresse, barr, off, len);
	  if (result != -1) return result;
      }

      return -1;
  }

  public int read (int adresse, byte[] barr, int off, int len)
  {
    int result = -1;
    if (simAccesParking != null)
    {
	 result =  simAccesParking.read(adresse, barr, off, len);
	 if (result != -1) return result;
     }
      return -1;
  }

  public int writeRead(int adresseI2c, byte[] wbarr, int woff, int wlen, byte[] rbarr, int roff, int rlen)
  {
    return -1;
  }

}